package com.ccx.demo.Handler;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class CustomfailureHandler implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        Map<String,Object> result=new HashMap<>();
        result.put("msg","登录失败");
        result.put("status",500);
        result.put("exception",e);
        httpServletResponse.setContentType("application/json;charset=UTF-8");
        String value = new ObjectMapper().writeValueAsString(result);
        httpServletResponse.getWriter().println(value);
    }
}
